package com.bjpowernode.money.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.money.mapper.BidInfoMapper;
import com.bjpowernode.money.mapper.FinanceAccountMapper;
import com.bjpowernode.money.mapper.IncomeRecordMapper;
import com.bjpowernode.money.mapper.LoanInfoMapper;
import com.bjpowernode.money.model.BidInfo;
import com.bjpowernode.money.model.IncomeRecord;
import com.bjpowernode.money.model.LoanInfo;
import com.bjpowernode.money.service.IncomeRecordService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/25
 */
@Service(interfaceClass = IncomeRecordService.class,version = "1.0.0",timeout = 20000)
@Component
public class IncomeRecordServiceImpl implements IncomeRecordService {

    @Autowired
    private LoanInfoMapper loanInfoMapper;

    @Autowired
    private BidInfoMapper bidInfoMapper;

    @Autowired
    private IncomeRecordMapper incomeRecordMapper;

    @Autowired
    private FinanceAccountMapper financeAccountMapper;

    @Override
    @Transactional
    public void incomeRecordService() {

        // 1、查询所有满标产品集合     即productStatus为1
        List<LoanInfo> loanInfoList = loanInfoMapper.selectLoanByFullStatus();

        // 2、遍历集合，查找产品的所有投资记录
        for (LoanInfo loanInfo : loanInfoList) {
            List<BidInfo> bidInfoList = bidInfoMapper.selectBidInfoById(loanInfo.getId());
            // 3、将投资记录生成收益计划
            for (BidInfo bidInfo : bidInfoList) {
                IncomeRecord incomeRecord = new IncomeRecord();
                incomeRecord.setLoanId(bidInfo.getLoanId());
                incomeRecord.setBidId(bidInfo.getId());
                Double bidMoney = bidInfo.getBidMoney();
                incomeRecord.setBidMoney(bidMoney);
                incomeRecord.setUid(bidInfo.getUid());
                // 收益状态（0未返，1已返）
                incomeRecord.setIncomeStatus(0);

                // 满标日期+周期      收益计算
                Date productFullTime = loanInfo.getProductFullTime();
                Integer cycle = loanInfo.getCycle();
                Integer productType = loanInfo.getProductType();
                Double rate = loanInfo.getRate();

                Date incomeDate = null;
                Double incomeMoney = null;
                // 判断是哪种产品
                if (productType == 0){      // 新手宝
                    incomeDate = DateUtils.addDays(productFullTime, cycle);
                    incomeMoney = rate / 100 / 365 * bidMoney * cycle;
                    incomeMoney = new BigDecimal(incomeMoney).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
                }else {
                    incomeDate = DateUtils.addMonths(productFullTime, cycle);
                    incomeMoney = rate / 100 / 365 * bidMoney * cycle * 30;
                    incomeMoney = new BigDecimal(incomeMoney).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
                }
                incomeRecord.setIncomeDate(incomeDate);
                incomeRecord.setIncomeMoney(incomeMoney);
                int num = incomeRecordMapper.insertSelective(incomeRecord);
                if (num == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }

            // 4、更改产品状态为 2：满标已生成收益计划
            loanInfo.setProductStatus(2);
            int num = loanInfoMapper.updateByPrimaryKeySelective(loanInfo);
            if (num == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }

        }


    }

    @Override
    @Transactional
    public void updateStatusAndFinanceByDate(Date currentTime) {
        // 查出所有在该时间之前的 收益记录
        List<IncomeRecord> incomeRecordList = incomeRecordMapper.selectIncomeRecordByDate(currentTime);
        for (IncomeRecord incomeRecord : incomeRecordList) {
            int num = incomeRecordMapper.updateStatusById(incomeRecord.getId());
            if (num == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
            Integer uid = incomeRecord.getUid();
            Double incomeMoney = incomeRecord.getIncomeMoney();
            num = financeAccountMapper.updateAvailableMoneyForIncome(uid,incomeMoney);
            if (num == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }

    }

}
